#pragma once

namespace ConvertString
{
	static const int MAXLENGTH_COMPSTRING = 256;
	INT ToWideChar( UINT codePage, const char* i_Text, int len, wchar_t* i_wText, int wLen )
	{
		int tLen = MultiByteToWideChar(codePage, 0, i_Text, len, 0, 0);

		if(tLen <= wLen)
		{
			return MultiByteToWideChar(codePage, 0, i_Text, len, i_wText, wLen);
		}

		return 0;
	}

	INT ToMultiByte( UINT codePage, const wchar_t* i_wText, int wLen, char* i_Text )
	{
		int tLen = WideCharToMultiByte(codePage, 0, i_wText, wLen, NULL, 0, NULL, NULL);

		if(tLen > MAXLENGTH_COMPSTRING) {
			return 0;
		} else {
			return WideCharToMultiByte(codePage, 0, i_wText, wLen, i_Text, MAXLENGTH_COMPSTRING, NULL, NULL);
		}
	}
}

namespace CodePages
{
	/*----------------------------------------------------------------------------*/ 
	int	GetCharsetFromLang( LANGID langid )
	{
		switch( PRIMARYLANGID(langid) )
		{
		case LANG_JAPANESE:
			return SHIFTJIS_CHARSET;
		case LANG_KOREAN:
			return HANGEUL_CHARSET;
		case LANG_CHINESE:
			switch( SUBLANGID(langid) )
			{
			case SUBLANG_CHINESE_SIMPLIFIED:
				return GB2312_CHARSET;
			case SUBLANG_CHINESE_TRADITIONAL:
				return CHINESEBIG5_CHARSET;
			default:
				return ANSI_CHARSET;
			}
		case LANG_GREEK:
			return GREEK_CHARSET;
		case LANG_TURKISH:
			return TURKISH_CHARSET;
		case LANG_HEBREW:
			return HEBREW_CHARSET;
		case LANG_ARABIC:
			return ARABIC_CHARSET;
		case LANG_ESTONIAN:
		case LANG_LATVIAN:
		case LANG_LITHUANIAN:
			return BALTIC_CHARSET;
		case LANG_THAI:
			return THAI_CHARSET;
		case LANG_CZECH:
		case LANG_HUNGARIAN:
		case LANG_POLISH:
		case LANG_CROATIAN:
		case LANG_MACEDONIAN:
		case LANG_ROMANIAN:
		case LANG_SLOVAK:
		case LANG_SLOVENIAN:
			return EASTEUROPE_CHARSET;
		case LANG_RUSSIAN:
		case LANG_BELARUSIAN:
		case LANG_BULGARIAN:
		case LANG_UKRAINIAN:
			return RUSSIAN_CHARSET;
		default:
			return ANSI_CHARSET;
		}
	}

	/*----------------------------------------------------------------------------*/ 
	int	GetCodePageFromCharset( int charset )
	{
		switch( charset )
		{
		case SHIFTJIS_CHARSET:
			return 932;
		case HANGUL_CHARSET:
			return 949;
		case GB2312_CHARSET:
			return 936;
		case CHINESEBIG5_CHARSET:
			return 950;
		case GREEK_CHARSET:
			return 1253;
		case TURKISH_CHARSET:
			return 1254;
		case HEBREW_CHARSET:
			return 1255;
		case ARABIC_CHARSET:
			return 1256;
		case BALTIC_CHARSET:
			return 1257;
		case THAI_CHARSET:
			return 874;
		case EASTEUROPE_CHARSET:
			return 1250;
		default:
			return 1252;
		}
	}

	/*----------------------------------------------------------------------------*/ 
	int	GetCodePageFromLang( LANGID langid )
	{
		return GetCodePageFromCharset(GetCharsetFromLang(langid));
	}
}